var videoFile = []; //文件流
var videoSrc = []; //图片路径
var videoName = []; //图片名字

function videoUploaderSetup() {

    videoFile = []; //文件流
    videoSrc = []; //图片路径
    videoName = []; //图片名字


    $('#imageUploaderContainer').hide();
    $('#videoUploaderContainer').show();
    $('#videoUploaderContainer .content-img-list li').each(function (index, value) {
        videoFile.push(new File(['text1', 'text2'], $(value).find('video').attr('src'), {type: 'text/plain'}));
        console.log($(value).find('video').attr('src'));
        videoSrc.push($(value).find('video').attr('src'));
        videoName.push('test.txt');
        if (videoSrc.length >= 1) {
            $('#videoUploaderContainer .content-img .file').hide();
        }
    });

    // 鼠标经过显示删除按钮
    $('#videoUploaderContainer .content-img-list').on('mouseover', '.content-img-list-item', function () {
        $(this).children('a').removeClass('layui-hide');
    });
    // 鼠标离开隐藏删除按钮
    $('#videoUploaderContainer .content-img-list').on('mouseleave', '.content-img-list-item', function () {
        $(this).children('a').addClass('layui-hide');
    });
    // 单个图片删除
    $("#videoUploaderContainer .content-img-list").on("click", '.content-img-list-item a', function () {
        var index = $(this).attr("index");
        console.log(this);
        videoSrc.splice(index, 1);
        videoFile.splice(index, 1);
        videoName.splice(index, 1);
        var boxId = ".content-img-list";
        addVideoNewContent(boxId);
        if (videoSrc.length < 1) {//显示上传按钮
            $('#videoUploaderContainer .content-img .file').show();
        }
    });

    $('#videoUploader').on('change', function () {
        if (videoSrc.length > 1) {
            return alert("最多只能上传1个视频");
        }
        console.log(this);
        var imgSize = this.files[0].size;  //b
        if (imgSize > 1024 * 1024 * 100) {//100M
            return alert("上传图片不能超过100M");
        }
        if (this.files[0].type != 'video/mp4') {
            return alert("视频上传格式不正确");
        }
        var imgBox = '.content-img-list';
        var fileList = this.files;
        for (var i = 0; i < fileList.length; i++) {
            var imgSrcI = getObjectURL(fileList[i]);
            videoName.push(fileList[i].name);
            videoSrc.push(imgSrcI);
            videoFile.push(fileList[i]);
        }
        if (videoSrc.length === 1) {//隐藏上传按钮
            $('#videoUploaderContainer .content-img .file').hide();
        }
        addVideoNewContent(imgBox);
        this.value = null;//解决无法上传相同图片的问题
    });
}

//图片展示
function addVideoNewContent(obj) {
    obj = '#videoUploaderContainer ' + obj;
    $(obj).html("");
    for (var a = 0; a < videoSrc.length; a++) {
        var oldBox = $(obj).html();
        $(obj).html(oldBox + '<li class="content-img-list-item"><video src="' + videoSrc[a] + '" controls="controls"></video>' +
            '<a index="' + a + '" class="layui-hide delete-btn"><i class="layui-icon layui-icon-delete"></i></a></li>');
    }
}